package com.practice.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.practice.common.constant.MessageConstant;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.admin.AdvimgMapper;
import com.practice.mapper.admin.AdvposMapper;
import com.practice.pojo.admin.dto.AdvposDTO;
import com.practice.pojo.admin.dto.AdvposPageDTO;
import com.practice.pojo.admin.entity.Advimg;
import com.practice.pojo.admin.entity.Advpos;
import com.practice.pojo.admin.vo.YddAdvposVO;
import com.practice.service.admin.AdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class AdvposServiceImpl extends ServiceImpl<AdvposMapper, Advpos> implements AdvposService {


    @Autowired
    private AdvposMapper advposMapper;

    @Autowired
    private AdvimgMapper advimgMapper;


    //广告位列表-分页查询
    @Override
    public Result pageList(AdvposPageDTO advposPageDTO) {
        //非空判断
        if (advposPageDTO.getPage()==null){
            //设置当前页默认值
            advposPageDTO.setPage(1);
        }
        if (advposPageDTO.getLimit()==null){
            //设置总页默认值
            advposPageDTO.setLimit(10);
        }

        //创建IPage分页对象,设置分页参数
        IPage<Advpos> page=new Page<>(advposPageDTO.getPage(),advposPageDTO.getLimit());

        //执行分页条件查询
        LambdaQueryWrapper<Advpos> lqw = new LambdaQueryWrapper<Advpos>();
        if(advposPageDTO.getAdvposdesc()!=null){
            lqw.like(Advpos::getAdvposdesc,advposPageDTO.getAdvposdesc());
        }
        if(advposPageDTO.getAdvposname()!=null){
            lqw.like(Advpos::getAdvposname,advposPageDTO.getAdvposname());
        }

        IPage<Advpos> page2 = advposMapper.selectPage(page, lqw);

        //拷贝集合page2.getRecords()并生成新的YddAdvposVO集合
        List<YddAdvposVO> voList = BeanUtil.copyToList(page2.getRecords(), YddAdvposVO.class);

        //遍历voList集合，根据广告位id查询广告位下广告图数量advposnum并赋值
        for (YddAdvposVO advposVO : voList) {
            LambdaQueryWrapper<Advimg> lqw2 = new LambdaQueryWrapper<Advimg>();
            lqw2.eq(Advimg::getAdvimgpos,advposVO.getId());
            Long count = advimgMapper.selectCount(lqw2);
            advposVO.setAdvposnum(count);
        }

        //page2对象属性值封装到PageResult对象内并返回
        PageResult pageResult = new PageResult();
        pageResult.setCount(page2.getTotal());
        pageResult.setTotalPages(page2.getPages());
        pageResult.setPageSize(page2.getSize());
        pageResult.setCurrentPage(page2.getCurrent());
        pageResult.setData(voList);

        return Result.success(pageResult);
    }



    //广告位修改
    @Override
    public Result advposUpdate(AdvposDTO advposDTO) {
        //1.非空判断
        if(advposDTO.getId()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }

        Advpos advpos = new Advpos();
        BeanUtils.copyProperties(advposDTO,advpos);

        advposMapper.updateById(advpos);

        return Result.success();
    }



    //广告位删除
    @Override
    public Result advposDelete(Integer id) {
        //非空判断
        if(id==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        advposMapper.deleteById(id);
        return Result.success();
    }


    //广告位列表-全部
    @Override
    public Result advposAll() {
        List<Advpos> advposList = advposMapper.selectList(null);
        return Result.success(advposList);
    }


    //广告位详情
    @Override
    public Result advposDetail(Integer id) {
        Advpos advpos = advposMapper.selectById(id);
        return Result.success(advpos);
    }


    //广告位添加
    @Override
    public Result advposAdd(AdvposDTO advposDTO) {
        //查询ydd_advpos表中id列的最大值
        Long maxId = advposMapper.selectMaxId();

        Advpos advpos = new Advpos();
        BeanUtils.copyProperties(advposDTO,advpos);
        advpos.setId(maxId.intValue()+1);
        advposMapper.insert(advpos);
        return Result.success();
    }
}
